#!/bin/bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"

source $SNAP/actions/common/utils.sh

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
    echo "This MicroK8s deployment is acting as a node in a cluster."
    echo "Use 'snap stop microk8s' to stop services on this node."
    exit 0
fi

exit_if_no_permissions

FORCE=false
PARSED=$(getopt --options=lho: --longoptions=force,help,output: --name "$@" -- "$@")
eval set -- "$PARSED"
while true; do
    case "$1" in
        -h|--help)
            echo "Usage: $0 [OPTIONS]"
            echo
            echo "Stop Kubernetes services"
            echo
            echo "Options:"
            echo " -h, --help          Show this help"
            exit 0
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "$0: invalid option -- $1"
            exit 1
    esac
done

if ! run_with_sudo snap stop ${SNAP_NAME} --disable
then
    echo 'Failed to stop microk8s services. Check snapd logs with "journalctl -u snapd.service"'
    exit 1
else
    kill_all_container_shims
    run_with_sudo touch ${SNAP_DATA}/var/lock/stopped.lock
fi
